/*
* when tick one region then tick all the tld checkboxs in this region
*/
function tick_all_tld(which_region,which_tlds)
{	region=Array('generic', 'europe', 'asia', 'america', 'africa');
	if(which_region=='region_All'){
		if(document.getElementById(which_region).checked){
			for( var i = 0; i < document.getElementsByName('regionTab').length; i++ ){
	  			document.getElementsByName('regionTab')[i].checked = true;
	  			$('#domainTagRegion_'+region[i]).attr('class','domainTagRegion');
			}
			for( var j = 0; j < region.length; j++ ){
				for( var i = 0; i < document.getElementsByName('domains_'+region[j]).length; i++ ){
	  				document.getElementsByName('domains_'+region[j])[i].checked = true;
				}
			}
		}else{
			for( var i = 0; i < document.getElementsByName('regionTab').length; i++ ){
	  			document.getElementsByName('regionTab')[i].checked = false;
	  			$('#domainTagRegion_'+region[i]).attr('class','domainTagRegion');
			}
			for( var j= 0; j < region.length; j++ ){
				for( var i = 0; i < document.getElementsByName('domains_'+region[j]).length; i++ ){
	  				document.getElementsByName('domains_'+region[j])[i].checked = false;
				}
			}
		}
		$('#domainTagRegion_all').attr('class','domainTagRegion current');
	}else{
		for( var i = 0; i < document.getElementsByName('regionTab').length; i++ ){
	  			$('#domainTagRegion_'+region[i]).attr('class','domainTagRegion');
		}
		if(document.getElementById(which_region).checked){
			for( var i = 0; i < document.getElementsByName(which_tlds).length; i++ ){
	  			document.getElementsByName(which_tlds)[i].checked = true;
			}
	    }else{
	     	for( var i = 0; i < document.getElementsByName(which_tlds).length; i++ ){
	  			document.getElementsByName(which_tlds)[i].checked = false;
			}
			//document.getElementById('regions_all').checked=false;	
	    }
	    $('#domainTagRegion_'+which_region.split('_')[1]).attr('class','domainTagRegion current');
	}
}
function tick_all_tlds(){
	if($('#region_All').attr('checked') == true){
		$('input[name="selectAll"]').each(function(){
			if($(this).attr('checked') == false)
				$(this).attr('checked',true);
		});
		$('input[name^="domains_"]').each(function(){
			if($(this).attr('name')!= 'domains_specials')
				$(this).attr('checked',true);
		});
	}else{
		$('input[name="selectAll"]').each(function(){
			if($(this).attr('checked') == true)
				$(this).attr('checked',false);
		});
		$('input[name^="domains_"]').each(function(){
			if($(this).attr('name')!= 'domains_specials')
				$(this).attr('checked',false);
		});

	}
		
}
function create_country_tldlist(){
	var tlds="";
	var onetldselected=false;
	var _tld = $('input[name="domain"]').val();
	if(_tld.indexOf('.') != -1){
		var index = parseInt(_tld.indexOf('.'));
		var name =_tld.substring(0,index);
		var len = parseInt(_tld.length);
		tlds = _tld.substring(index+1,len)+'_';
	}
	
	var e = document.getElementsByName("domains");
	for(var j=0;j<e.length;j++)
	{
		if(e[j].checked==true){
			//tlds+=e[j].value+"|";
			var tldtxt = e[j].value;
			tldtxt = (tldtxt).substr(1, tldtxt.length-1)+"_";
			tlds+= tldtxt;			
			onetldselected=true;
		}
	 }
	 
	 if(!onetldselected){
		alert("Please select a TLD!");
	}else{
	document.getElementById("tlds").value=tlds.substring(0,tlds.length-1);
	document.form_domain_check.submit();
	}
}
function create_tldlist(){
	var region=Array('specials', 'generic', 'europe', 'asia', 'america', 'africa');
	var tlds="";
	var onetldselected=false;
	var chooseDotCO = false;
	var e=document.getElementsByName("region[]");
	var domain_name=jQuery.trim(document.getElementById('_domain').value);
	domain_name = domain_name.toLocaleLowerCase();
	if(domain_name.substr(0,4) == 'www.')
		domain_name = domain_name.substr(4,domain_name.length);
	if(domain_name.indexOf('.') != -1){
		onetldselected=true;
		tlds+=jQuery.trim(domain_name.substring(domain_name.indexOf('.')+1))+"_";
  	}
	
	for(var j=0;j<e.length;j++)
	{
		if(e[j].checked==true){
			onetldselected=true;
		}
	}
	
	for(var i=0;i<region.length;i++)
	{
		var e=document.getElementsByName("domains_"+region[i]);
		for(var j=0;j<e.length;j++)
		{
			if(e[j].checked==true){
				var tldtxt = e[j].value;
				if(tldtxt == '.co')
					chooseDotCO = true;
				tldtxt = (tldtxt).substr(1, tldtxt.length-1)+"_";
				tlds+= tldtxt;
				//tlds+=e[j].value+"_";
				onetldselected=true;
			}
		 }
   }
	if(chooseDotCO)
		tlds = sortTldList(tlds); 
   if(!onetldselected){
   		alert("Please select a TLD!");
   }else{
   	  tlds = tlds.replace(/co/g,'K');
   	  tlds= tlds.replace(/xn--55qx5d/g,'A');
   	  tlds= tlds.replace(/xn--gmqw5a/g,'B');
   	  tlds= tlds.replace(/xn--mgbaam7a8h/g,'C');
   	  tlds= tlds.replace(/xn--od0alg/g,'D');
   	  tlds= tlds.replace(/xn--uc0atv/g,'E');
   	  tlds = tlds.replace(/biz/g,'F');
   	  tlds = tlds.replace(/com/g,'G');
   	  tlds = tlds.replace(/net/g,'H');
   	  tlds = tlds.replace(/org/g,'I');
   	  tlds = tlds.replace(/info/g,'J');
   	  tlds = tlds.replace(/mobi/g,'L');
	   document.getElementById("tlds").value=tlds.substring(0,tlds.length-1);
	   
	   document.getElementById("domain").value=domain_name;
	   document.frmToSubmit.submit();
	}
}
function sortTldList(tldList){
	//this will put the .co to the second positon
	var tldArr = tldList.split('_');
	var length = tldArr.length-1;
	var tldStr = '';
	if(length == 1)
		return tldList;
	else{
		for(var i = 0;i<length;i++){
			if(tldArr[i] == 'co'){
				tldArr.splice(i,1);
				break;
			}	
		}
		tldArr.splice(1,0,'co');
		for(var j=0;j<tldArr.length-1;j++){
			tldStr += tldArr[j]+'_';
		}
		return tldStr;
	}
}

if(typeof $ != 'undefined') {	
	$.fn.getBgImgSize = function () {
		if($(this).css('background-image') != '' && $(this).css('background-image').toLowerCase().indexOf('url(') != -1) {				
		    var path = $(this).css('background-image').replace('url(', '').replace('url(', '').replace(/[\'\"\)]+/g, '');
		    var tempImg = '<img id="tempImg" src="' + path + '"/>';
		    $('body').append(tempImg); // add to DOM before </body>
		    $('#tempImg').hide(); //hide image
		    var dimension = [$('#tempImg').width(),$('#tempImg').height()]; //get dimension
		    $('#tempImg').remove(); //remove from DOM
		    return dimension;
		}
		return '';
	};
}

function disableInputs(isDisabled) {
	$(':input').each(function(){
		if(typeof isDisabled != 'undefined' && !isDisabled) {
			if($(this).is('[type="button"],[type="submit"]') && $(this).is('[class^="btn"]')) {
				
				if(typeof $(this).attr('origStyle') != 'undefined')	{				
					$(this).attr('style',$(this).attr('origStyle'));
					$(this).removeAttr('origStyle');
				} else
					$(this).removeAttr('style');											
			
				$(this).removeAttr('disabled');
			} else {
				$(this).removeAttr('readonly');
			}
		} else {
			if($(this).is('[type="button"],[type="submit"]') && $(this).is('[class^="btn"]')) {
				if($(this).css('display') != 'none') {
					
					if(typeof $(this).attr('style') != 'undefined')
						$(this).attr('origStyle',$(this).attr('style'));
					
					disableButton($(this));
				}
			} else {
				$(this).attr('readonly','readonly');				
			}		
		}
	});
}

function disableButton(jObj) {	
	var dimension = jObj.getBgImgSize();		
	
	var posX = 0;
	if(jObj.css('background-position')) {
		posX = jObj.css('background-position');
		posX = posX.substring(0,posX.indexOf(' '));						
	}
	
	jObj.attr('style','background-position: ' + posX + ' -' + parseInt(dimension[1]/2) + 'px !important; ' + 
			'box-shadow: none !important; cursor: default !important; border: 1px solid #B1B1B1;' + 
			'color: #888888 !important');
	
	jObj.attr('disabled','disabled');	
}

function loadJsScript(src,callback,addToHeader) {
	var script = document.createElement('script');
	if(script.readyState) {		
		script.src = src;
		script.type = 'text/javascript';
		script.onreadystatechange = function(){
			if(this.readyState === 'complete' || this.readyState === 'loaded')
				eval(callback);
		};
	} else {
		script.setAttribute('type','text/javascript');
		script.setAttribute('src',src);
		script.onload = function(){eval(callback);};
	}
	
	if(addToHeader == undefined || addToHeader == true)
		document.getElementsByTagName('head')[0].appendChild(script);
	
	return script;
}
function create_tld_list(){
	var region=Array('specials', 'generic', 'europe', 'asia', 'america', 'africa');
	var tlds="";
	var onetldselected=false;
	var chooseDotCO = false;
	var e=document.getElementsByName("region[]");
	var domain_name=jQuery.trim(document.getElementById('_domain').value);
	domain_name = domain_name.toLocaleLowerCase();
	if(domain_name.substr(0,4) == 'www.')
		domain_name = domain_name.substr(4,domain_name.length);
	if(domain_name.indexOf('.') != -1){
		onetldselected=true;
		tlds+=jQuery.trim(domain_name.substring(domain_name.indexOf('.')+1))+"_";
  	}
	
	for(var j=0;j<e.length;j++)
	{
		if(e[j].checked==true){
			onetldselected=true;
		}
	}
	
	for(var i=0;i<region.length;i++)
	{
		var e=document.getElementsByName("domains_"+region[i]);
		for(var j=0;j<e.length;j++)
		{
			if(e[j].checked==true){
				var tldtxt = e[j].value;
				if(tldtxt == '.co')
					chooseDotCO = true;
				tldtxt = (tldtxt).substr(1, tldtxt.length-1)+"_";
				tlds+= tldtxt;
				//tlds+=e[j].value+"_";
				onetldselected=true;
			}
		 }
   }
	if(chooseDotCO)
		tlds = sortTldList(tlds); 
   if(!onetldselected){
   		alert("Please select a TLD!");
   }else{
   	  tlds= tlds.replace(/xn--55qx5d/g,'A');
   	  tlds= tlds.replace(/xn--gmqw5a/g,'B');
   	  tlds= tlds.replace(/xn--mgbaam7a8h/g,'C');
   	  tlds= tlds.replace(/xn--od0alg/g,'D');
   	  tlds= tlds.replace(/xn--uc0atv/g,'E');
   	  tlds = tlds.replace(/biz/g,'F');
   	  tlds = tlds.replace(/com/g,'G');
   	  tlds = tlds.replace(/net/g,'H');
   	  tlds = tlds.replace(/org/g,'I');
   	  tlds = tlds.replace(/info/g,'J');
   	  tlds = tlds.replace(/co/g,'K');
   	  tlds = tlds.replace(/mobi/g,'L');
   		
	   document.getElementById("tldList").value=tlds.substring(0,tlds.length-1);
	   document.getElementById("domainName").value=domain_name;
	   document.frmToSubmit.submit();
	}
}
function checkAll(){
	$(':checkbox[name="domains_specials"]').each(function(){
		if($(this).attr('checked'))
			$(this).attr('checked',false);
		else
			$(this).attr('checked',true);
	});
	var value = $('#checkAllAtag').html();
	if(value == 'Check All')
		$('#checkAllAtag').html('Uncheck All');
	else
		$('#checkAllAtag').html('Check All');
}

function check_All(){
                var value = $('#checkAllAtag').html();
                var flag = false;
                if(value == '( Check All )'){
                                flag = true;
                                $('#checkAllAtag').html('( Uncheck All )');
                }else{
                                flag = false;
                                $('#checkAllAtag').html('( Check All )');
                }
                $(':checkbox[name="domains_specials"]').each(function(){
                                if(flag){
                                                $(this).attr('checked',true);
                                }else{
                                                $(this).attr('checked',false);
                                }
                });
}


function FormSubmit(){
	var comeFrom = $('#comeFrom').val();
	if(comeFrom == 'bulkSearch'){
		$('#formDomainSearch').submit();
		return true;
	}else{
		var premiumFlag = false;
		var suggestionFlag = false;
		if(checkSuggestionDomains())
			suggestionFlag = true;
		if(needSubmitPremiumDomain()){
			premiumFlag = true;
			var domain_name = '';
			$('input:checkbox[name="domain_Name[]"]').each(function(){
				if($(this).attr('checked'))
					domain_name += $(this).val()+'+'; 
			});
			$('#premiumDomainList').val(domain_name);
		}
		if(!checkForm() && !premiumFlag && !suggestionFlag){
			alert('Please select domain(s)!');
			return false;
		}else{
			$('#formDomainSearch').submit();
			return true;
		}
	}
}
function checkForm(){
	var whetherChoosed = false;
	$('#domainSearch input:checkbox').each(function(){
    	if($(this).attr('checked'))
    	   whetherChoosed = true;
    });
	return whetherChoosed;
}
function needSubmitPremiumDomain(){
	var _whetherChoosed = false;
	$('#domain_Search input:checkbox').each(function(){
    	if($(this).attr('checked'))
    	   _whetherChoosed = true;
    });
	return _whetherChoosed;
}
function checkSuggestionDomains(){
	var _whetherChoosed = false;
	$('#suggestionedDomain input:checkbox').each(function(){
    	if($(this).attr('checked'))
    	   _whetherChoosed = true;
    });
	return _whetherChoosed;
}
function validateWhois(){
	var domain = /^[^_\~!@#\$%\+\^&\*\(\)\[\]\{\}<>\?\\\/\'\s\"]+$/;
	if(domain.test($('#searchField').val())==false){
		alert("Wrong domain name, please enter again\r\n");
		return false;
	}else
		return true;
}